const app = getApp();
const utils = require('../../../utils/util.js');
Page({
  data: {
    CurrentPage: 1,
    loadingMore: 1,
    cityId: 0,
    cateId: 0,
    lng: 0,
    lat: 0,
    key: '',
    shopList: [],   //店铺列表
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    this.setData({
      cityId: options.cityId,
      cateId: options.cateId,
      key: options.key,
    })

    utils.getLocation((location)=>{
      that.data.lng = location.lng;
      that.data.lat = location.lat;
      that.search();
     },app);  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },
  onReachBottom: function () {   
      this.loadMore();
  },
  
  //商家搜索
  search: function(){
    let that = this;
    if(that.data.key==''){
      utils.showToast('请输入门店或商品名称'); return;
    }
    if(that.data.lng<=0){
      utils.showToast('请开启手机定位'); return;
    }
    if(that.data.cityId<=0){
      utils.showToast('缺少城市参数'); return;
    }
    wx.showLoading({title: '加载中...',mask: true})
    wx.request({
      url: app.globalData.host + '/api/meituan/search' ,
      data: {
        key: that.data.key,
        cityId: that.data.cityId,
        sortType: 1,
        lng: that.data.lng,
        lat: that.data.lat,
        page: that.data.CurrentPage
      },
      success: function (res) {
        wx.hideLoading();
        if (res.data.Code == 0) {
          let shopList = that.data.shopList;
          for(let i = 0; i < res.data.Data.Items.length; i++){           
            let item =  res.data.Data.Items[i];
            item.DealBaseInfo.realPrice = (item.DealBaseInfo.realPrice/100).toFixed(2);
            item.DealBaseInfo.marketPrice = (item.DealBaseInfo.marketPrice/100).toFixed(0);
            item.ShopInfo.pricePerson = (item.ShopInfo.pricePerson/100).toFixed(0);
            item.ShopInfo.shopPowers=[];         
            for(let j=0; j<50; j+= 10){
              let num = item.ShopInfo.shopPower - j;
              if(num >= 10){
                item.ShopInfo.shopPowers.push('star10.png'); //满星
              }else if(num < 10 & num > 0){
                item.ShopInfo.shopPowers.push('star5.png');  //半颗星
              }else if(num < 10){
                item.ShopInfo.shopPowers.push('star0.png');  //没星
              }             
            }
            if(item.ShopInfo.distance > 1000){
              item.ShopInfo.distance = Math.round(item.ShopInfo.distance/100)/10  + ' km'
            }else{
              item.ShopInfo.distance += ' m';
            }
            if(!that.isShopExists(shopList,item.ShopInfo.shopName)){
              shopList.push(item);
            }            
          }
          that.setData({
            shopList: shopList,
            loadingMore: res.data.Data.PageModel.TotalPages
          })
          
          if(that.data.CurrentPage < 3 && that.data.shopList.length < 5){
            that.data.CurrentPage++;
            setTimeout(() => {
              that.search();
            }, 100);
          }
        } else {
          utils.showModal(res.data.Message);
        }
      }
    })
   
  },
  //店铺详情
  toDetail: function(e){
    let that = this;
    let index = e.currentTarget.dataset.index;
    let key = that.data.shopList[index].ShopInfo.shopName;  //按店铺店铺名称搜索
    wx.navigateTo({
      url: '/pages/meituan/detail/index?cityId=' + that.data.cityId + '&cateId=' + that.data.cateId + '&key=' + key,
    })
  },
  //加载更多
  loadMore: function () {
    let that = this;
    if (that.data.loadingMore > 1 && that.data.CurrentPage < that.data.loadingMore) {
      var currPage = that.data.CurrentPage;
      that.setData({
        CurrentPage: currPage + 1
      })
      that.search();
    }
  },
  //门店是否存在
  isShopExists: function(shopList,shopName){
    for(let i = 0; i < shopList.length; i++){
      if(shopList[i].ShopInfo.shopName == shopName){
        return true;
      }
    }
    return false;
  },
  bindKey: function(e){
    this.setData({
      key: e.detail.value
    })
  }
})